package com.xncoding.rocketmq.Listener;

import com.xncoding.rocketmq.entity.Order;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

/**
 *consumeMode 控制消费模式，您可以选择并发或有序接收消息。
 */
//@RocketMQMessageListener事务消息监听器
//consumeMode 消费模式,默认值 ConsumeMode.CONCURRENTLY 并行处理;ConsumeMode.ORDERLY 按顺序处理
//messageModel 消息模型,默认值 MessageModel.CLUSTERING 集群;MessageModel.BROADCASTING 广播
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.producer.groupName1}", topic = "ORDER_ADD",messageModel = MessageModel.CLUSTERING, consumeMode = ConsumeMode.ORDERLY)
public class OrderMqListener implements RocketMQListener<Order> , RocketMQPushConsumerLifecycleListener {
    @Override
    public void onMessage(Order order) {
        System.out.println("接收到消息，开始消费..OrderType:" + order.getOrderType() + ",OrderNO:" + order.getOrderNO());
    }
    //    该方法重写消息监听器的属性
    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
// 设置消费者消息重试次数
        consumer.setMaxReconsumeTimes(3);
        //        设置实例名称
//        defaultMQPushConsumer.setInstanceName("mqconsumer1");
    }
}
